package org.sction.manager.database;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

/**
 * @author 作者 : sorc
 * @version 创建时间：2012-3-31 上午11:47:10
 * 
 *          类说明
 */
public class PaginationHelper<E> {
	private static final Logger log = Logger.getLogger(PaginationHelper.class);

	public CurrentPage<E> fetchPage(final JdbcTemplate jt,
			final String sqlCountRows, final String sqlFetchRows,
			final Object args[], final int pageNo, final int pageSize,
			final ParameterizedRowMapper<E> rowMapper) {
		log.info("sqlCount:" + sqlCountRows);
		log.info("sqlPage:" + sqlFetchRows);
		log.info("pageNo:" + pageNo);
		final int rowCount = jt.queryForInt(sqlCountRows, args);
		final CurrentPage<E> page = new CurrentPage<E>(pageNo, pageSize,
				rowCount);
		jt.query(sqlFetchRows, args, new ResultSetExtractor() {
			public Object extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				final List<E> pageItems = page.getPageItems();
				int currentRow = 0;
				while (rs.next()
						&& currentRow < page.getBeginRecordNum() + pageSize) {
					if (currentRow >= page.getBeginRecordNum()) {
						pageItems.add(rowMapper.mapRow(rs, currentRow));
					}
					currentRow++;
				}
				return page;
			}
		});
		return page;
	}

	public static CurrentPage<Map<String, Object>> fetchPage(
			final JdbcTemplate jt, final String sqlCountRows,
			final String sqlFetchRows, final Object args[], final int pageNo,
			final int pageSize) {
		log.info("sqlCount:" + sqlCountRows);
		log.info("sqlPage:" + sqlFetchRows);
		log.info("pageNo:" + pageNo);
		int rowCount = jt.queryForInt(sqlCountRows, args);
		final CurrentPage<Map<String, Object>> page = new CurrentPage<Map<String, Object>>(
				pageNo, pageSize, rowCount);

		jt.query(sqlFetchRows, args, new ResultSetExtractor() {
			public Object extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				final List<Map<String, Object>> pageItems = page.getPageItems();
				int currentRow = 0;
				ResultSetMetaData srmd = rs.getMetaData();
				while (rs.next()
						&& currentRow < page.getBeginRecordNum() + pageSize) {
					if (currentRow >= page.getBeginRecordNum()) {
						int i = srmd.getColumnCount();
						Map<String, Object> map = new HashMap<String, Object>();
						while (i > 0) {
							map.put(srmd.getColumnLabel(i), rs.getObject(i));
							i--;
						}
						pageItems.add(map);
					}
					currentRow++;
				}
				return page;
			}
		});
		return page;
	}

	@SuppressWarnings("unchecked")
	public static CurrentPage<Map<String, Object>> fetchPageBySql(
			final JdbcTemplate jt, final String sqlCountRows,
			final String sqlFetchRows, final Object args[], final int pageNo,
			final int pageSize) {
		log.info("sqlCount:" + sqlCountRows);
		log.info("sqlPage:" + sqlFetchRows);
		log.info("pageNo:" + pageNo);
		int rowCount = jt.queryForInt(sqlCountRows, args);
		final CurrentPage<Map<String, Object>> page = new CurrentPage<Map<String, Object>>(
				pageNo, pageSize, rowCount);
		page.setPageItems(jt.queryForList(sqlFetchRows, args));
		return page;
	}
}
